<?php
/*********************/
/*                   */
/*  后台优惠券历史模块   */
/*   BY voiet        */
/*   2009-9-19更新    */
/*                   */
/*********************/
//require_once( CORE_DIR."/model/trading/mdl.coupon.php" );
class cmd_coupon extends mdl_coupon
{
	function getcouponuid($coupon_id){
	
		$row = $this->db->selectrow( "select member_id from sdb_member_coupon where memc_code=\"".$coupon_id."\"" );		
		return $row['member_id'];
	
	}
	
	function generatecoupon( $cpnsId, $userId, $nums, $orderId = "" )
	{
		$curTime = time( );
		$sSql = "select * from sdb_coupons as c\n            left join sdb_promotion as p on c.pmt_id=p.pmt_id\n            where cpns_status='1' and cpns_type='1' and c.cpns_id=".$cpnsId." and\n   pmt_time_end >".$curTime;
		if ( $aCoupon = $this->db->selectrow( $sSql ) )
		{
			$i = 1;
			for ( ;	$i <= $nums;	++$i	)
			{
				if ( $couponCode = $this->_makecouponcode( $aCoupon['cpns_gen_quantity'] + $i, $aCoupon['cpns_prefix'], $aCoupon['cpns_key'] ) )
				{
					$aData = array(
						"memc_code" => $couponCode,
						"cpns_id" => $cpnsId,
						"member_id" => $userId,
						"memc_gen_orderid" => $orderId,
						"memc_gen_time" => time( )
					);
					$rRs = $this->db->query( "SELECT * FROM sdb_member_coupon WHERE 0=1" );
					$sSql = $this->db->getinsertsql( $rRs, $aData );
					$this->db->exec( $sSql );
					$aData = array(
						"cpns_gen_quantity" => $aCoupon['cpns_gen_quantity'] + $i
					);
					$rRs = $this->db->query( "SELECT * FROM sdb_coupons WHERE cpns_id=".intval( $cpnsId ) );
					$sSql = $this->db->getupdatesql( $rRs, $aData );
					if ( !$sSql )
					{
						continue;
					}
					$this->db->exec( $sSql );
				}
				else
				{
					return false;
				}
			}
			return true;
		}
		return false;
	}

}

?>